package com.enjoyor.soa.traffic.frame.support.util;

import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

public class HttpClientUtil {

    public static String client(String url, HttpMethod method, MultiValueMap<String, String> params) {
        RestTemplate client = new RestTemplate();
        HttpHeaders headers = new HttpHeaders();
        // 请勿轻易改变此提交方式，大部分的情况下，提交方式都是表单提交
        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
        HttpEntity<MultiValueMap<String, String>> requestEntity = new HttpEntity<MultiValueMap<String, String>>(params,
                headers);
        // 执行HTTP请求
        ResponseEntity<String> response = client.exchange(url, HttpMethod.POST, requestEntity, String.class);
        return response.getBody();
    }

    public static void main(String[] args) {

        HttpMethod method = HttpMethod.POST;
        MultiValueMap<String, String> params = new LinkedMultiValueMap<String, String>();
        params.add("token", "c68260a8-e9d2-4faf-b2b9-3ad2f3e4913b");
        params.add("systemKey", "a7e8ff20-678a-40bd-986b-9f0ee7fec307");
        // 发送http请求并返回结果
        String result = HttpClientUtil.client("http://192.168.91.152:8081/uums-server/token/check", method, params);
        System.out.println(result);
    }

}
